//
//  AddContentViewController.m
//  ReciteBook
//
//  Created by xuziyuan on 13-12-27.
//  Copyright (c) 2013年 student. All rights reserved.
//

#import "AddContentViewController.h"
#import "Content.h"

@interface AddContentViewController ()

@end

@implementation AddContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(IBAction)backgroundTap:(id)sender
{
//    [self.sortTextField resignFirstResponder];
    [self.menuView.textField resignFirstResponder];
    [self.titleTextField resignFirstResponder];
    [self.authorTextField resignFirstResponder];
    [self.poesyTextField resignFirstResponder];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem=rightBtn;
    self.menuView=[[MenuView alloc] initWithFrame:CGRectMake(120, 16, 159, 30)];
    self.menuView.textField.placeholder=@"请输入体裁";
    NSArray *arr=[[NSArray alloc] initWithObjects:@"唐诗",@"宋词",@"古体诗", nil];
    self.menuView.tableArray=arr;
    [self.view addSubview:self.menuView];
}
-(void)save
{
    Content *newone=(Content *)[NSEntityDescription insertNewObjectForEntityForName:@"Content" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
    newone.sort=self.menuView.textField.text;
    newone.title=self.titleTextField.text;
    newone.author=self.authorTextField.text;
    newone.poesy=self.poesyTextField.text;
    
    self.menuView.textField.text=@"";
    self.titleTextField.text=@"";
    self.authorTextField.text=@"";
    self.poesyTextField.text=@"";
    NSLog(@"体裁=%@,题目=%@,作者=%@,内容=%@",newone.sort,newone.title,newone.author,newone.poesy);
    
    NSError *error;
    if (![self.myAppDelegate.managedObjectContext save:&error]) {
        NSLog(@"保存成功");
    }else {
        NSLog(@"%@ %@",error,error.userInfo);
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
